all: build run report test

build: dirs
	cd build && cmake -DCMAKE_BUILD_TYPE=Release ..
	cd build && make -j4

run: build test_all

report: 
	cd doc && xelatex -interaction=batchmode report.tex

dirs:
	mkdir -p build
	mkdir -p figures
	mkdir -p results

test: build
	@echo "------- Running tests -------"
	cd build && ./main
	@echo "------- End tests -------"

# 新增批量测试目标
test_all: build
	@echo "------- 开始批量测试所有求解器 -------"
	chmod +x ./test_all_solvers.sh
	./test_all_solvers.sh
	@echo "------- 批量测试完成 -------"

# 测试指定求解器
test_solver: build
	@echo "------- 测试指定求解器 -------"
	@if [ -z "$(SOLVER)" ]; then \
		echo "请指定SOLVER参数，例如: make test_solver SOLVER='AdamsBashforth[1]' STEP=0.01"; \
		exit 1; \
	fi
	@if [ -z "$(STEP)" ]; then \
		cd build && ./main -t 1 -n "$(SOLVER)" -o "$(SOLVER)_result.txt" && python ../src/plot.py "$(SOLVER)_result.txt"; \
	else \
		cd build && ./main -t 1 -n "$(SOLVER)" -s $(STEP) -o "$(SOLVER)_$(STEP)_result.txt" && python ../src/plot.py "$(SOLVER)_$(STEP)_result.txt"; \
	fi
	@echo "------- 测试完成 -------"

clean:
	rm -rf build
	rm -f ./doc/*.aux ./doc/*.log ./doc/*.pdf ./doc/*.fdb_latexmk ./doc/*.fls ./doc/*.synctex.gz ./doc/*.xdv
	rm -rf figures
	rm -rf results
	rm -f *.png
	rm -f output.txt

clean_results:
	rm -rf results
	rm -f ./doc/*.aux ./doc/*.log ./doc/*.fdb_latexmk ./doc/*.fls ./doc/*.synctex.gz ./doc/*.xdv
	rm -rf figures
	rm -f *.png
	rm -f output.txt

.PHONY: all build run report dirs test test_all test_solver clean